package com.syj.core.variable;

import com.syj.web.model.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author rankai
 *         createTime 2017-02-2017/2/8 14:37
 */
public class SessionUtil {

    public static HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        return request;
    }

    public static User getUser() {
        HttpSession session = getRequest().getSession();
        if (session != null) {
            return (User) session.getAttribute(GlobalParameters.USER_SESSION);
        }
        return null;
    }

    public Integer getUserId() {
        User user = getUser();
        return user == null ? null : user.getId();
    }
}
